(defun c:CriaGrid (/ p1 p2 temp_xmin temp_ymin temp_xmax temp_ymax xmin ymin xmax ymax intx inty txth x y texto baseX baseY oldsnap oldlayer)
  (princ "\n--- Criador de Malha com COORDENADAS INTEIRAS (Versão 3) ---")

  ;; Salva configurações atuais
  (setq oldsnap (getvar "osmode"))
  (setq oldlayer (getvar "clayer"))

  ;; Inputs do usuário
  (if (and
        (setq p1 (getpoint "\nClique no canto inferior esquerdo da área desejada: "))
        (setq p2 (getcorner p1 "\nClique no canto superior direito da área desejada: "))
        (setq intx (getreal "\nIntervalo em X (use um número inteiro): "))
        (setq inty (getreal "\nIntervalo em Y (use um número inteiro): "))
        (setq txth (getreal "\nAltura do texto: "))
      )
    (progn
      (princ "\n*Ajustando a área da malha para as coordenadas inteiras mais próximas...*")
      
      ;; --- ALTERAÇÕES PARA COORDENADAS INTEIRAS ---

      ;; 1. Captura os limites da área clicada pelo usuário
      (setq temp_xmin (min (car p1) (car p2)))
      (setq temp_ymin (min (cadr p1) (cadr p2)))
      (setq temp_xmax (max (car p1) (car p2)))
      (setq temp_ymax (max (cadr p1) (cadr p2)))

      ;; 2. Arredonda os limites para os inteiros mais próximos que englobem a área
      ;;    'fix' remove a parte decimal, efetivamente arredondando para baixo (floor).
      (setq xmin (fix temp_xmin))
      (setq ymin (fix temp_ymin))
      
      ;;    Para arredondar para cima (ceiling), verificamos se já é um inteiro.
      ;;    Se não for, somamos 1 e pegamos a parte inteira.
      (setq xmax (fix temp_xmax))
      (if (> temp_xmax xmax) (setq xmax (1+ xmax)))
      
      (setq ymax (fix temp_ymax))
      (if (> temp_ymax ymax) (setq ymax (1+ ymax)))

      ;; 3. Define as coordenadas base como os novos valores inteiros
      (setq baseX xmin)
      (setq baseY ymin)
      ;; ------------------------------------------------

      (setvar "osmode" 0) ; desliga snap

      ;; Cria camada única
      (command "_-LAYER" "M" "MALHA-LINHAS-INTEIRAS" "C" "8" "" "")
      (command "_-LAYER" "S" "MALHA-LINHAS-INTEIRAS" "")

      ;; Linhas verticais
      (setq x xmin)
      (while (<= x xmax)
        (command "_LINE" (list x ymin) (list x ymax) "")
        (setq x (+ x intx))
      )

      ;; Linhas horizontais
      (setq y ymin)
      (while (<= y ymax)
        (command "_LINE" (list xmin y) (list xmax y) "")
        (setq y (+ y inty))
      )

      ;; Textos nas bordas
      ;; Parte inferior e superior
      (setq x xmin)
      (while (<= x xmax)
        ;; --- ALTERAÇÃO: Formata o texto para 0 casas decimais ---
        (setq texto (rtos (+ baseX (- x xmin)) 2 0))
        (command "_TEXT" "Style" "Standard" "Justify" "TC" (list x (- ymin (* 1.5 txth))) txth "0" texto)
        (command "_TEXT" "Style" "Standard" "Justify" "BC" (list x (+ ymax (* 0.5 txth))) txth "0" texto)
        (setq x (+ x intx))
      )

      ;; Lateral esquerda e direita
      (setq y ymin)
      (while (<= y ymax)
        ;; --- ALTERAÇÃO: Formata o texto para 0 casas decimais ---
        (setq texto (rtos (+ baseY (- y ymin)) 2 0))
        (command "_TEXT" "Style" "Standard" "Justify" "MR" (list (- xmin (* 0.5 txth)) y) txth "0" texto)
        (command "_TEXT" "Style" "Standard" "Justify" "ML" (list (+ xmax (* 0.5 txth)) y) txth "0" texto)
        (setq y (+ y inty))
      )

      ;; Restaura variáveis
      (setvar "osmode" oldsnap)
      (command "_-LAYER" "S" oldlayer "")
      (princ "\nMalha com coordenadas inteiras criada com sucesso.")
    )
    (princ "\nComando cancelado.")
  )
  (princ)
)